package com.example.beikeyigou;

import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.beikeyigou.dialog.PromptDialog;
import com.example.beikeyigou.login_reg.LoginRegActivity;
import com.example.beikeyigou.login_reg.SelfDataChangeActivity;
import com.example.beikeyigou.login_reg.UserBean;
import com.example.beikeyigou.utils.SaveUserUtils;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RadioGroup radioGroup;
    private RadioButton radioButton1, radioButton2, radioButton3, radioButton4;
    private Button fpageBtn, typeBtn, meBtn, loginBtn, regBtn, cancelBtn;
    private TextView loginTv;
    private DrawerLayout drawerLayout;

    private UserBean.DataBean userData;     // 之前登录时保存的用户信息

    // 将当前Activity包含的fragment放到一个集合里，便于管理
    List<Fragment> fragmentList;
    private FragmentManager fm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findView();
        initData();
        setListener();
        initFragment();
        changeStatusBarTextImgColor(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        refreshData();
    }

    // 修改底部导航栏的图标大小
    private void changeBarImageSize() {
        Drawable drawable1 = getResources().getDrawable(R.drawable.main_rb1);
        //第一0是距左右边距离，第二0是距上下边距离，第三50长度,第四宽度50
        drawable1.setBounds(0, 0, 65, 65);
        //只放上面
        radioButton1.setCompoundDrawables(null, drawable1, null, null);

        Drawable drawable2 = getResources().getDrawable(R.drawable.main_rb2);
        //第一0是距左右边距离，第二0是距上下边距离，第三50长度,第四宽度50
        drawable2.setBounds(0, 0, 65, 65);
        //只放上面
        radioButton2.setCompoundDrawables(null, drawable2, null, null);

        Drawable drawable3 = getResources().getDrawable(R.drawable.main_rb3);
        //第一0是距左右边距离，第二0是距上下边距离，第三50长度,第四宽度50
        drawable3.setBounds(0, 0, 65, 65);
        //只放上面
        radioButton3.setCompoundDrawables(null, drawable3, null, null);

        Drawable drawable4 = getResources().getDrawable(R.drawable.main_rb4);
        //第一0是距左右边距离，第二0是距上下边距离，第三50长度,第四宽度50
        drawable4.setBounds(0, 0, 65, 65);
        //只放上面
        radioButton4.setCompoundDrawables(null, drawable4, null, null);
    }

    private void initData() {
        refreshData();
        changeBarImageSize();
    }

    public void changeStatusBarTextImgColor(boolean color) {
        if (color) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//实现状态栏图标和文字颜色为暗色
        } else {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);//恢复状态栏白色字体
        }
    }

    // 刷新用户数据
    private void refreshData() {
        userData = SaveUserUtils.getUserData(this);
        if (userData == null) {
            loginTv.setText("请先登录");
        } else {
            loginTv.setText(userData.getNickname() + "\n欢迎您！");
        }
    }

    // 初始化fragmentList
    private void initFragment() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new FPageFragment());
        fragmentList.add(new TypeFragment());
        fragmentList.add(new TrolleyFragment());
        fragmentList.add(new MeFragment());

        // 将碎片添加到activity中
        // 1. 获取碎片管理者对象
        fm = getSupportFragmentManager();
        // 2. 获取碎片事物对象
        FragmentTransaction transaction = fm.beginTransaction();
        for (int i = 0; i < fragmentList.size(); ++i) {
            transaction.add(R.id.main_layout, fragmentList.get(i));
            transaction.hide(fragmentList.get(i));
        }
        transaction.show(fragmentList.get(0));  //默认显示第一个
        transaction.commit();
    }
    // 切换Fragment
    private void changeFragment(int pos) {
        FragmentTransaction transaction = fm.beginTransaction();
        for (int i = 0; i < fragmentList.size(); ++i) {
            transaction.hide(fragmentList.get(i));
        }
        transaction.show(fragmentList.get(pos));  //默认显示第一个
        transaction.commit();
    }

    // 侧栏的按钮监听器
    View.OnClickListener onClickListenerSide = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.drawer_btn_fpage:
                    radioGroup.check(R.id.main_rb1);
                    drawerLayout.closeDrawers();    // 关闭抽屉
                    break;
                case R.id.drawer_btn_type:
                    radioGroup.check(R.id.main_rb2);
                    drawerLayout.closeDrawers();    // 关闭抽屉
                    break;
                case R.id.drawer_btn_me:
                    radioGroup.check(R.id.main_rb4);
                    drawerLayout.closeDrawers();    // 关闭抽屉
                    break;
                case R.id.drawer_btn_login:
                    // 跳转页面
                    // 判断是否登录了，如果登录了就跳转到个人中心页面；如果没有，则跳转到登录页面
                    isLogin(0);
                    break;
                case R.id.drawer_btn_reg:
                    isLogin(1);
                    break;
                case R.id.drawer_btn_cancel:
                    exitUser();
                    drawerLayout.closeDrawers();    // 关闭抽屉
                    break;
                default:
                    break;
            }
        }
    };

    private void exitUser() {
        userData = SaveUserUtils.getUserData(this);
        if (userData == null) {
            Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show();
        } else {
            PromptDialog promptDialog = new PromptDialog(this);
            promptDialog.show();
            promptDialog.setPromptTv("确认退出？")
                    .setOnClickBottomListener(new PromptDialog.OnClickBottomListener() {
                        @Override
                        public void onPositiveClick() {
                            SaveUserUtils.deleteUserData(MainActivity.this);
                            refreshData();
                            fragmentList.get(2).onResume();
                            fragmentList.get(3).onResume();
                            promptDialog.dismiss();
                        }

                        @Override
                        public void onNegtiveClick() {
                            promptDialog.dismiss();
                        }
                    });
        }
    }

    private void isLogin(int flag) {
        refreshData();
        Intent intent;
        if (userData == null) {
            intent = new Intent(MainActivity.this, LoginRegActivity.class);
            intent.putExtra("flag", flag);
            startActivity(intent);
            return;
        }
        intent = new Intent(MainActivity.this, SelfDataChangeActivity.class);
        startActivity(intent);
    }

    // 设置监听器
    private void setListener() {
        fpageBtn.setOnClickListener(onClickListenerSide);
        typeBtn.setOnClickListener(onClickListenerSide);
        meBtn.setOnClickListener(onClickListenerSide);
        loginBtn.setOnClickListener(onClickListenerSide);
        regBtn.setOnClickListener(onClickListenerSide);
        cancelBtn.setOnClickListener(onClickListenerSide);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.main_rb1:
                        changeFragment(0);
                        break;
                    case R.id.main_rb2:
                        changeFragment(1);
                        break;
                    case R.id.main_rb3:
                        changeFragment(2);
                        break;
                    case R.id.main_rb4:
                        changeFragment(3);
                        break;
                    default:
                        break;
                }
            }
        });
    }

    // 查找控件
    private void findView() {
        radioGroup = findViewById(R.id.main_rg);
        fpageBtn = findViewById(R.id.drawer_btn_fpage);
        typeBtn = findViewById(R.id.drawer_btn_type);
        meBtn = findViewById(R.id.drawer_btn_me);
        loginBtn = findViewById(R.id.drawer_btn_login);
        regBtn = findViewById(R.id.drawer_btn_reg);
        cancelBtn = findViewById(R.id.drawer_btn_cancel);
        loginTv = findViewById(R.id.drawer_tv_name);
        drawerLayout = findViewById(R.id.main_drawer);

        radioButton1 = findViewById(R.id.main_rb1);
        radioButton2 = findViewById(R.id.main_rb2);
        radioButton3 = findViewById(R.id.main_rb3);
        radioButton4 = findViewById(R.id.main_rb4);
    }
}